//
//  LoopTabBarController.swift
//  LoopWeibo
//
//  Created by loop安 on 2016/12/6.
//  Copyright © 2016年 loop. All rights reserved.
//

import UIKit


class LoopTabBarController: UITabBarController,UITabBarControllerDelegate {
    
    var reloadInfo = {() -> () in
     
    
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()

        let loopTarBar = LoopTarBar()
        self.setValue(loopTarBar, forKey: "tabBar")
        self.delegate = self
        
        //有循环引用,需要制作一个弱替身
        loopTarBar.composeClosure = { [weak self] in
            print("撰写按钮被点击了")
            print(self!)
            let compose = LoopComposeView()
//            UIApplication.shared.keyWindow?.addSubview(compose)
            compose.show(vc: self!)
        }

        creatChildControllers()
    }

    func creatChildControllers() {
        
        creatChildController(vc : LoopHomeViewController(), title : "Home", imageName : "tabbar_home",index:0)
        
        creatChildController(vc : LoopMessageViewController(), title : "Message", imageName : "tabbar_message_center",index:1)
        
        creatChildController(vc : LoopDiscoverViewController(), title : "Discover", imageName : "tabbar_discover",index:2)
        
        creatChildController(vc : LoopMeViewController(), title : "Me", imageName : "tabbar_profile",index:3)
    
    }
    
    func creatChildController(vc:UIViewController, title:String, imageName:String,index:Int) {
        //创建控制器
        //        let vc :UIViewController = UIViewController.init()
        //        vc.title = "123"
        
        vc.tabBarItem.title = title
        vc.navigationItem.title = title
        vc.tabBarItem.tag = index
        //设置tabbar图片
        vc.tabBarItem.image = UIImage(named : imageName)?.withRenderingMode(.alwaysOriginal)
        vc.tabBarItem.selectedImage = UIImage(named: imageName + "_selected")?.withRenderingMode(.alwaysOriginal)
        //设置tabbar文字属性
        vc.tabBarItem.setTitleTextAttributes([NSForegroundColorAttributeName : UIColor.orange], for: .selected)
        vc.tabBarItem.setTitleTextAttributes([NSFontAttributeName : UIFont.systemFont(ofSize: 11)], for: .normal)
        vc.tabBarItem.titlePositionAdjustment = UIOffsetMake(0, -3)
        //创建navigationControll
        let nav:LoopNaviController = LoopNaviController.init(rootViewController: vc)
        
        self.addChildViewController(nav)
        
    }
    
    override func tabBar(_ tabBar: UITabBar, didSelect item: UITabBarItem) {
        var index = 0
        
        for subBtn in tabBar.subviews {
            if subBtn.isKind(of: NSClassFromString("UITabBarButton")!) {
                if index == item.tag {
                    for view in subBtn.subviews {
                        if view.isKind(of: NSClassFromString("UIImageView")!) {
                            view.transform = CGAffineTransform.init(scaleX: 0.6, y: 0.6)
                            UIView.animate(withDuration: 0.025,
                                           delay: 0.025,
                                           usingSpringWithDamping: 0.3,
                                           initialSpringVelocity: 5,
                                           options: [],
                                           animations: { 
                                            view.transform = CGAffineTransform.identity
                            },
                                           completion: { (_) in
                                            
                            })
                        }
                    }
                }
                index += 1
            }
        }
    }
    
    func tabBarController(_ tabBarController: UITabBarController, shouldSelect viewController: UIViewController) -> Bool{
        
        let vc = tabBarController.selectedViewController
        
        if viewController.isEqual(vc){
            reloadInfo()
        }
        return true
    }
}
